package it.gameplanet.client.widgets;

import it.gameplanet.client.GamePlanetContext;
import it.gameplanet.client.utils.AbstractAsyncCallback;
import it.gameplanet.client.widgets.articolo.CreaArticolo;
import it.gameplanet.client.widgets.support.ContextMenuLabel;
import it.gameplanet.client.widgets.support.Dialogs;
import it.gameplanet.model.Articolo;
import it.gameplanet.model.Categoria;
import it.gameplanet.service.GamePlanetService;

import java.util.HashMap;
import java.util.Map;

import com.google.gwt.event.dom.client.BlurEvent;
import com.google.gwt.event.dom.client.BlurHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyDownEvent;
import com.google.gwt.event.dom.client.KeyDownHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.TextBox;

public class CategoriaTreeItem extends Composite {

	private final ContextMenuLabel labelCategoria;
	private final TextBox renameTB;
	private final SimplePanel container;

	private final Command inserisciArticoloCommand = new Command() {
		  public void execute() {
				Articolo articolo = new Articolo();
				articolo.setCategoria(categoria.getCompositeFormat());
				GamePlanetContext.setContentPanel(new CreaArticolo(articolo));
		  }
	};
	private final Command cancellaCategoriaCommand = new Command() {
		  public void execute() {
			    
				GamePlanetService.Utils.getGamePlanetServiceAsync().eliminaCategoria(categoria.getNome(), categoria.getParent(), new AbstractAsyncCallback<Void>(){

					@Override
					public void onSuccess(Void result) {
						RootPanel.get("contentContainer").clear();
						Dialogs.showMessage("Categoria "+categoria.getNome()+ " cancellato con successo");
						GamePlanetContext.getArticoliTree().reload();	
					}
				});
		  }
	};
	private final Command rinominaCategoriaCommand = new Command() {
		  public void execute() {

			    renameTB.setText(labelCategoria.getText());
			    final EventHandler handler = new EventHandler();
				renameTB.addValueChangeHandler(handler);
			    renameTB.addKeyDownHandler(handler);
			    renameTB.addBlurHandler(handler);
			    
			    container.clear();
			    container.add(renameTB);

			    renameTB.setFocus(true);
		  }
	};

	final class EventHandler implements ValueChangeHandler<String>, KeyDownHandler, BlurHandler {

		@Override
		public void onValueChange(
				final ValueChangeEvent<String> event) {
		    doRename();
		}

		@Override
		public void onKeyDown(KeyDownEvent event) {
			if(event.getNativeKeyCode()==KeyCodes.KEY_ENTER) {
				doRename();
			}
			else {
				
			}
			
			switch(event.getNativeKeyCode()) {
				case KeyCodes.KEY_ENTER:
					doRename();
					break;
				case KeyCodes.KEY_ESCAPE:
					
			}
		}

		@Override
		public void onBlur(BlurEvent event) {
			doRename();
		}				
		
		private void doRename() {
			GamePlanetService.Utils.getGamePlanetServiceAsync().rinominaCategoria(labelCategoria.getText(), renameTB.getText(), new AbstractAsyncCallback<Void>() {

				@Override
				public void onSuccess(Void result) {
				}
		    	
		    });

		    labelCategoria.setText(renameTB.getText());
		    categoria.setNome(renameTB.getText());
			container.clear();
		    container.add(labelCategoria);
		}


	}

	protected Map<String, Command> getMenuBar() {
		
		Map<String, Command> menu = new HashMap<String, Command>();
		
		menu.put("Inserisci Articolo", inserisciArticoloCommand);
		menu.put("Rinomina Categoria", rinominaCategoriaCommand);
		menu.put("Cancella Categoria", cancellaCategoriaCommand);
		
		
		return menu;
	}
	
	final Categoria categoria;
	
	public CategoriaTreeItem(final Categoria c) {
		
		this.categoria = c;

		container = new SimplePanel();
		renameTB = new TextBox();
		labelCategoria = new ContextMenuLabel(categoria.getNome(), getMenuBar());
		
		container.add(labelCategoria);
		initWidget(container);
	}

	public Categoria getCategoria() {
		return categoria;
	}
}
